//
//  WYCommentView.m
//  Boss
//
//  Created by admin on 2018/6/8.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCommentView.h"
@interface WYCommentView()<UITextViewDelegate>

@property (nonatomic,strong)UILabel *countLabel;

@end
@implementation WYCommentView

-(instancetype)init
{
    if (self=[super init]) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews
{
    self.backgroundColor =[UIColor hexStringToColor:@"#fdfdff" alpha:1.0];
    UIView *topView = [[UIView alloc]init];
    [self addSubview:topView];
   // topView.backgroundColor = [UIColor whiteColor];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *comment = [[UILabel alloc]init];
    [topView addSubview:comment];
    [comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(10);
    }];
    comment.font = kFontSystemBIgger2(15);
    comment.textColor = kColor999999;
    comment.text = @"评论";
    self.comment = comment;
    UIButton *closeBtn = [[UIButton alloc]init];
    [topView addSubview:closeBtn];
    UIView *lineView = [[UIView alloc]init];
    [topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(kLineHeight);
    }];
    lineView.backgroundColor = [UIColor hexStringToColor:@"#e6e6e6" alpha:1.0];
    topView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
    
    
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(10);
        make.width.height.mas_equalTo(22);
    }];
    [closeBtn setImage:[UIImage imageNamed:@"comment_close-icon"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(clickCloseAction:) forControlEvents:(UIControlEventTouchUpInside)];
    CMInputView *textView = [[CMInputView alloc]initWithFrame:CGRectMake(16, 40, kScreenWidth-32, 80)];
    textView.delegate = self;
    textView.inputAccessoryView = [[UIView alloc]init];
    self.textView = textView;
    textView.font = [UIFont systemFontOfSize:15];
    [self addSubview:textView];
    textView.placeholder = @"禁止在评论区发广告";
    textView.placeholderFont = [UIFont systemFontOfSize:15];
    textView.placeholderColor = kColoraaaaaa;
    
    self.countLabel = [[UILabel alloc]init];
    [self addSubview:self.countLabel];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(textView.mas_right).offset(-8);
        make.bottom.equalTo(textView.mas_bottom).offset(-8);
    }];
    self.countLabel.text = @"100";
    self.countLabel.textColor = kColor999999;
    self.countLabel.font = [UIFont systemFontOfSize:13];
    
    
    
    
    
    
    
    
    
    //[textView becomeFirstResponder];
    UIButton *sendBtn = [[UIButton alloc]init];
    [self addSubview:sendBtn];
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.equalTo(textView.mas_bottom).offset(12);
    }];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:kColorMainColor forState:UIControlStateNormal];
    sendBtn.titleLabel.font = kFontSystemBIgger2(16);
    
    [sendBtn addTarget:self action:@selector(sendAction) forControlEvents:(UIControlEventTouchUpInside)];
    
}

-(void)textViewDidChange:(UITextView *)textView
{
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    if (existTextNum > 100){
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        NSString *s = [nsTextContent substringToIndex:100];
        //[textView setAttributedText: [self textViewAttributedStr:s]];
        textView.text = s;
    }
    //不让显示负数
    self.countLabel.text = [NSString stringWithFormat:@"%ld",MAX(0,100 - existTextNum)];
}









-(void)clickCloseAction:(UIButton *)btn
{
    [self.textView resignFirstResponder];
    
    if ([self.delegate respondsToSelector:@selector(clickCloseButton)]) {
        [self.delegate clickCloseButton];
    }
    self.textView.text = @"";
    self.textView.placeholderView.hidden = NO;
}
-(void)sendAction
{
    NSLog(@"1111111");
    [self.textView resignFirstResponder];
   
    if ([self.delegate respondsToSelector:@selector(clickSendButton)]) {
        [self.delegate clickSendButton];
    }
    self.textView.text = @"";
    self.textView.placeholderView.hidden = NO;
    
}

+(void)testAction
{
    NSLog(@"2222222");
    //[self.textView becomeFirstResponder];
}



@end

